package com.yituo.mjworld.mvc.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.yituo.mjworld.core.web.core.BaseForm;
import com.yituo.mjworld.core.web.core.Result;
import com.yituo.mjworld.mvc.entity.web.WebUser;
import com.yituo.mjworld.mvc.form.DeleteForm;
import com.yituo.mjworld.mvc.form.QueryForm;
import com.yituo.mjworld.mvc.service.BaseService;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.math.BigInteger;

/**
 * 数据统计模块
 */
@Service
public class DataServiceImpl extends BaseService {


  public int getMyUserNum(WebUser user) {

    String countSql = "select count(id) from web_user where referrer=:referrer";

    BigInteger count = (BigInteger) this.baseDao.getSession().createNativeQuery(countSql).setParameter("referrer", user.getId()).uniqueResult();

    return count.intValue();
  }

  /**
   * 获取我的子用户列表
   */
  public JSONObject getMyUser(QueryForm queryForm, WebUser user) {
    String countSql = "select count(id) from web_user where referrer=:referrer";
    String sql = "select  id" +
        "account," +
        "password," +
        "nickname," +
        "wxId," +
        "headImage," +
        "telphone," +
        "email," +
        "registerDate," +
        "lastLoginDate," +
        "int rule," +
        "scale " +
        "from web_user where referrer=:referrer ";
    queryForm.getParam().clear();
    queryForm.getParam().put("referrer", user.getId());
    return get(sql, countSql, queryForm, queryForm.getParam());
  }

  /**
   * 获取我的子用户列表
   */
  public JSONObject getSaleData(QueryForm queryForm, WebUser user) {
    String countSql = "select count(id) from web_user where referrer=:referrer";
    String sql = "select  " +
        "a.id" +
        "a.account," +
        "a.nickname," +
        "a.wxId," +
        "a.headImage," +
        "a.telphone," +
        "a.email," +
        "a.registerDate," +
        "a.lastLoginDate," +
        "a.rule," +
        "a.scale," +
        "(select count(b.id) from web_user where b.id=a.id)" +// 子代理推广人数
        "from web_user a  where a.referrer=:referrer ";
    queryForm.getParam().clear();
    queryForm.getParam().put("referrer", user.getId());
    return get(sql, countSql, queryForm, queryForm.getParam());
  }

  @Override
  public JSONObject getList(QueryForm form) {
    return null;
  }

  @Override
  public Result delete(DeleteForm form) {
    return null;
  }

  @Override
  public Result saveOrUpdate(BaseForm baseForm) throws InvocationTargetException, IllegalAccessException {
    return null;
  }
}
